﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_SetUserBasicReferenceValue')
	BEGIN
		DROP  Procedure  esf_sso.P_SetUserBasicReferenceValue
	END

GO

CREATE Procedure esf_sso.P_SetUserBasicReferenceValue
	@pUserPersonID			INT
	,@pBasicReferenceID		INT
	,@pValue				VARCHAR (1000) = NULL
AS

	IF @pValue IS NULL OR LEN (@pValue) = 0
	BEGIN
		DELETE FROM esf_sso.User_BasicReference_Values 
		WHERE UserPersonID = @pUserPersonID AND BasicReferenceID = @pBasicReferenceID
	END
	ELSE
	BEGIN

		IF NOT EXISTS (SELECT TOP 1 1 FROM esf_sso.User_BasicReference_Values WHERE BasicReferenceID = @pBasicReferenceID AND UserPersonID = @pUserPersonID)
		BEGIN
			INSERT INTO esf_sso.User_BasicReference_Values (BasicReferenceID, UserPersonID, Value)
			VALUES (@pBasicReferenceID, @pUserPersonID, @pValue)
		END
		ELSE
		BEGIN
			UPDATE esf_sso.User_BasicReference_Values SET Value = @pValue
			WHERE BasicReferenceID = @pBasicReferenceID AND UserPersonID = @pUserPersonID
		END

	END

GO